package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        //根据用户名查询用户对象
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(DtsAdmin::getUsername, username);

        DtsAdmin admin = adminMapper.selectOne(queryWrapper);

        return admin;
    }

    @Override
    public IPage<DtsAdmin> querypage(Integer page, Integer limit, String sort, String order, String username) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);

        QueryWrapper<DtsAdmin> qw  = new QueryWrapper<>();

        if(!StringUtils.isEmpty(username)){
            qw.like("username", username);
        }

        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else{
                qw.orderByAsc(sort);
            }
        }

        adminMapper.selectPage(iPage, qw);

        return iPage;
    }

    @Override
    public DtsAdmin add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        String password = admin.getPassword();
        DtsAdmin adminDB = adminMapper.selectById(admin.getId());
        String passwordDb = adminDB.getPassword();

        if(!password.equalsIgnoreCase(passwordDb)){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(encode);
        }

        admin.setUpdateTime(new Date());

        adminMapper.updateById(admin);
        return admin;
    }

    @Override
    public void delete(DtsAdmin admin) {
        adminMapper.deleteById(admin.getId());
    }

    @Override
    public List<Map<String, Object>> selectAdminList() {
        List<DtsAdmin> dtsAdmins = adminMapper.selectList(null);
        List<Map<String, Object>> adminList = dtsAdmins.stream().map(dtsAdmin -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsAdmin.getId());
            map.put("label", dtsAdmin.getUsername());
            return map;
        }).collect(Collectors.toList());
        return adminList;
    }
}
